from urllib import error, request
import chardet
from urllib3.util.url import parse_url
import gzip
import ssl
ssl._create_default_https_context = ssl._create_unverified_context


def ask_url(url, headers=None):
    uri = parse_url(url)
    if headers == None:
        headers = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
            "Accept-Encoding": "gzip, deflate, br",
            "Host": uri.host,
            "Referer": f"{uri.scheme}://{uri.host}/",
        }
    req = request.Request(url, headers=headers)
    html = ""
    while True:
        try:
            res = request.urlopen(req)
            html = res.read()
            html = decode_html(html)
        except error.URLError as e:
            if hasattr(e, "code"):
                print(e.code)
            if hasattr(e, "reason"):
                print(e.reason)
        if html != "":
            break
        print(f"获取{url}失败，重新获取")

    return html


def decode_html(html):
    result = ''
    try:
        if html.startswith(b'\x1f\x8b\x08'):
            html = gzip.decompress(html)
        charset = chardet.detect(html)["encoding"]
        if charset != None:
            result = html.decode(charset)
        else:
            result = html.decode("utf-8")
    except UnicodeDecodeError as e:
        print(e)
    return result
